




/*class Animal{
    public String name;
    public int age;
    public int a = 555;



    public void eat(){
        System.out.println(this.name + "正在吃饭");
    }
    public void sleep(){
        System.out.println(this.name + "正在睡觉");
    }
}

class Dog extends Animal{
    public String name;
    public int age;
    public int a = 666;


    void bark(){
        System.out.println(this.name + "汪汪汪~~~");
    }
}


class Cat extends Animal{

    void mew(){
        System.out.println(this.name + "喵喵喵~~~");
    }
}
// TestExtend.java
public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        System.out.println(dog.name);
        System.out.println(dog.age);
        System.out.println(dog.a);
        System.out.println(dog.b);
    }
}*/




class Animal {
    public String name;
    public int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat() {
        System.out.println(this.name + " 正在吃饭");
    }
}

class Cat extends Animal {

    public Cat(String name, int age) {
        super(name, age);
    }

    public void miaomaio() {
        System.out.println(this.name + "喵喵叫~");
    }

    @Override
    public void eat() {
        System.out.println(this.name + " 正在吃猫粮！");
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


class Dog extends Animal {
    public int age = 12;

    public Dog(String name, int age) {
        super(name, age);
    }

    public void bark() {
        System.out.println(this.name + "旺旺叫~");
    }

    @Override
    public void eat() {
        System.out.println(this.name + " 正在吃狗粮！");
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}



public class Test {

    public static void main7(String[] args) {
        Animal animal = new Dog("圆圆", 18);// 直接赋值，发生向上转型
        //animal.bark();//编译错误，不能调用

        /*Dog dog = (Dog) animal;// 向下转型
        dog.bark(); //*/

        /*Cat cat = (Cat) animal;
        cat.miaomaio();*/

        // 如果animal 引用的对象 是Cat对象 的实例
        if (animal instanceof Cat) {
            Cat cat = (Cat) animal;
            cat.miaomaio();
        } else {
            System.out.println("哈哈，我悟了！！！");
        }
    }


    //动态绑定 发生多态
    public static void eatFun(Animal animal) {
        animal.eat();
        //animal.bark(); //编译错误，不能调用
    }

    public static void main6(String[] args) {
        Dog dog = new Dog("圆圆", 19);
        eatFun(dog);

        Cat cat = new Cat("菲菲", 16);
        eatFun(cat);
    }


    // 方法重写
    public static void main5(String[] args) {

        Animal animal = new Dog("圆圆", 18);// 直接赋值，发生向上转型
        animal.eat();//调用子类重写的eat()方法，此时代码底层发生了动态绑定

        Cat cat = new Cat("菲菲", 16);
        cat.eat();
        /*Animal animal1 = new Cat("菲菲", 16);
        animal.eat();
        System.out.println(animal1);

        Animal animal2 = new Animal("feifei", 12);
        System.out.println(animal2);*/
    }


    public static void main4(String[] args) {

        Animal animal = new Dog("圆圆", 18);// 直接赋值，发生向上转型
        animal.eat();//

        Dog dog = new Dog("圆圆", 18);
        System.out.println(dog);

    }







    //作为方法的参数，传参的时候发生向上转型
    public static void func2(Animal animal) {

    }


    // 作为返回值 向上转型
    public static Animal func() {
        Dog dog = new Dog("圆圆", 18);
        return dog;
    }

    public static void main3(String[] args) {
        Dog dog = new Dog("圆圆", 18);
        //func2(dog);

    }



    /**
     * 什么是向上转型
     *
     * @param args
     */
    public static void main2(String[] args) {
        Dog dog = new Dog("圆圆",18);
        //animal 这个引用 指向了 dog这个引用 所指的对象
        Animal animal = dog; //因为继承关系，可以赋值

        Animal animal1 = new Dog("圆圆", 18);// 直接赋值，发生向上转型
        animal.eat();// 此时调用的是父类自己的eat
    }


    public static void main1(String[] args) {
        Dog dog = new Dog("圆圆", 18);
        dog.eat();
        dog.bark();
        System.out.println("========================");

        Animal animal = new Animal("花花",10);
        animal.eat();
        //animal.bark;//编译报错，子类独有的一个方法，不能通过父类调用

        //结论：通过父类引用，只能调用父类自己特有的成员方法或者变量，不能调用子类的
    }

}
